﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMLogistics.Model;
using WMLogistics_Dao;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Data;

namespace WMLogistics.Service
{
    public class AccountService
    {
        private AccountDao dao = new AccountDao();
        public wuliu_account_base Login(string account, string password, string ip)
        {
            var user = dao.GetAccount(account, password);
            //if (user != null)
            //{
            //    wuliu_account_system_login loginInfo = new wuliu_account_system_login();
            //    loginInfo.login_datetime = DateTimeToStamp(DateTime.Now);
            //    loginInfo.account_id = user.account_id;
            //    loginInfo.login_ip=ip;
            //    Task.Run(() => { dao.RecordLoginInfo(loginInfo); });
            //}
            return user;
        }
        public object PhoneLogin(string account,string password)
        {
            return dao.GetPhoneAccount(account, password);
        }


        public wuliu_account_base GetByAccountId(string accountid)
        {
            return dao.GetByAccountId(accountid);
        }

        public int IsExistPhone(string phone)
        {
            return dao.IsExistPhone(phone);
        }

       public bool DelAndroidPhoneToken(string account, string android_token)
        {
            return dao.DelAndroidPhoneToken(account, android_token);
        }
       public bool AddAndroidPhoneToken(wuliu_account_android_token mod)
       {
           return dao.AddAndroidPhoneToken(mod);
       }
       public bool DelIOSPhoneToken(string account, string ios_token)
       {
           return dao.DelIOSPhoneToken(account, ios_token);
       }
       public bool AddIOSPhoneToken(wuliu_account_ios_token mod)
       {
           return dao.AddIOSPhoneToken(mod);
       }

        /// <summary>
        /// 通过用户名获取帐号
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public wuliu_account_base GetAccountById(string account)
        {
            var user = dao.GetAccountById(account);
            return user;
        }

        // DateTime时间格式转换为Unix时间戳格式
        private int DateTimeToStamp(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return (int)(time - startTime).TotalSeconds;
        }

        public bool CheckLogin(string account, string password)
        {
            return dao.CheckAccount(account, password);
        }

        public List<wuliu_account_base> GetContactsList(int companyid)
        {
            return dao.GetLxList(companyid);
        }


        public int GetParentId(int accountId)
        {
            return dao.GetParentId(accountId);
        }

        public wuliu_account_base GetCompanyId(int account_id)
        {
            return dao.GetCompanyId(account_id);
        }

        public PageData<wuliu_account_base> GetListByCompanyId(int company_id,int page,int pageSize)
        {
            return dao.GetListByCompanyId(company_id,page,pageSize);
        }

        public bool Add(wuliu_account_base account)
        {
            return dao.Add(account);
        }

        public bool Add(wuliu_account_online online)
        {
            return dao.Add(online);
        }

        public bool Update(wuliu_account_base account)
        {
            return dao.Update(account);
        }

        public wuliu_account_online IsCheckUserToken(string token, int usertime)
        {
            return dao.IsCheckUserToken(token, usertime);
        }

        public int UpdateState(int id, int state)
        {
            return dao.UpdateState(id,  state);
        }
    }
}
